package com.wie.staffhubpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wie.staffhubpro.constant.MessageConstant;
import com.wie.staffhubpro.domain.dto.LeavaApplicationDTO;
import com.wie.staffhubpro.domain.dto.PermitLeaveAppDTO;
import com.wie.staffhubpro.domain.dto.SearchLeaveAppDTO;
import com.wie.staffhubpro.domain.entity.Emp;
import com.wie.staffhubpro.domain.entity.LeaveApplication;
import com.wie.staffhubpro.domain.vo.LeaveApplicationVO;
import com.wie.staffhubpro.exception.EmployeeNotExistException;
import com.wie.staffhubpro.mapper.EmpMapper;
import com.wie.staffhubpro.service.LeaveApplicationService;
import com.wie.staffhubpro.mapper.LeaveApplicationMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author WiedersehenM
* @description 针对表【leave_application】的数据库操作Service实现
* @createDate 2025-06-30 22:48:36
*/
@Service
public class LeaveApplicationServiceImpl extends ServiceImpl<LeaveApplicationMapper, LeaveApplication>
    implements LeaveApplicationService{

    @Autowired
    private LeaveApplicationMapper leaveApplicationMapper;

    @Autowired
    private EmpMapper empMapper;

    /**
     * 提交请假申请
     */
    @Transactional
    public void submit(LeavaApplicationDTO leavaApplicationDTO) {
        LeaveApplication leaveApplication = new LeaveApplication();
        BeanUtils.copyProperties(leavaApplicationDTO, leaveApplication);
        // 计算结束日期
        leaveApplication.setStartDate(LocalDate.now());
        leaveApplication.setEndDate(leaveApplication.getStartDate().plusDays(leaveApplication.getDays()));
        // 查找该员工的 deptId
        Emp emp = empMapper.selectById(leavaApplicationDTO.getEmpId());
        if (emp == null) {
            throw new EmployeeNotExistException(MessageConstant.EMPLOYEE_NOT_EXIST);
        }
        // 向数据库中插入信息
        leaveApplication.setDeptId(emp.getDeptId());
        leaveApplication.setStatus("待审批");
        leaveApplication.setCreateTime(LocalDateTime.now());
        leaveApplication.setUpdateTime(LocalDateTime.now());
        leaveApplicationMapper.insert(leaveApplication);
    }

    /**
     * 查询个人请假信息
     */
    public List<LeaveApplication> getByEmpId(SearchLeaveAppDTO searchLeaveAppDTO) {
        // 构造查询条件
         LambdaQueryWrapper<LeaveApplication> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(LeaveApplication::getEmpId, searchLeaveAppDTO.getEmpId())
                 .eq(searchLeaveAppDTO.getStatus() != null, LeaveApplication::getStatus, searchLeaveAppDTO.getStatus());
        return leaveApplicationMapper.selectList(wrapper);
    }

    /**
     * 查询部门请假信息
     */
    @Override
    public List<LeaveApplication> getByDeptId(SearchLeaveAppDTO searchLeaveAppDTO) {
        // 构造查询条件
        LambdaQueryWrapper<LeaveApplication> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaveApplication::getDeptId, searchLeaveAppDTO.getDeptId())
                .eq(searchLeaveAppDTO.getStatus() != null, LeaveApplication::getStatus, searchLeaveAppDTO.getStatus());
        return leaveApplicationMapper.selectList(wrapper);
    }

    /**
     * 审批请假申请
     */
    public void permit(PermitLeaveAppDTO permitLeaveAppDTO) {
        // 封装 LeaveApplication
        LeaveApplication leaveApplication = new LeaveApplication().builder()
                .id(permitLeaveAppDTO.getId())
                .approveDate(LocalDate.now())
                .status(permitLeaveAppDTO.getStatus())
                .updateTime(LocalDateTime.now())
                .build();
        leaveApplicationMapper.updateById(leaveApplication);
    }
}




